import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import Users from '@/components/user/Users'
import Categories from '@/components/goods/Categories'
import Goods from '@/components/goods/Goods'
import Params from '@/components/goods/Params'
import Orders from '@/components/order/Orders'
import Reports from '@/components/report/Reports'
import Rights from '@/components/root/Rights'
import Roles from '@/components/root/Roles'
import Add from '@/components/goods/add'

Vue.use(VueRouter)

  const routes = [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: Users
        },
        {
          path: '/categories',
          component: Categories
        },
        {
          path: '/goods',
          component: Goods,
        },
        {
          path: '/goods/add',
          component: Add
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/orders',
          component: Orders
        },
        {
          path: '/reports',
          component: Reports
        },
        {
          path: '/rights',
          component: Rights
        },
        {
          path: '/roles',
          component: Roles
        },
        
      ]
    },
    {
      path: '/login',
      component: Login
    },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由全局守卫
router.beforeEach((to, from, next) => {
  if(to.path === '/login') {
    return next()
  }else {
    console.log('守卫盘查')
    // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if(!tokenStr) {
      return next('/login')
    }else {
      next()
    }

  }

})
export default router
